//
//  DUSwitchView.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/21.
//

import Cocoa

@IBDesignable
class DUSwitchView: NSView, DUSwitchProtocol, SimpleClickHandler{


    var preButton:DUImgButton!
    var nextButton:DUImgButton!
    var monthButton:DUMonthButton!
    
    private var date:DateEntity!
    
    convenience init() {
        self.init(frame: CGRect.zero)
    }

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize () {
        
        preButton = DUImgButton()
        preButton.frame = NSRect(x: 10, y: 2, width: 40, height: 40)
        preButton.image = NSImage(named: "preIcon")
        preButton.onlyId = "preButton"
        preButton.click = self
        addSubview(preButton)
        nextButton = DUImgButton()
        nextButton.frame = NSRect(x: 329, y: 2, width: 40, height: 40)
        nextButton.image = NSImage(named: "nextIcon")
        nextButton.onlyId = "nextButton"
        nextButton.click = self
        addSubview(nextButton)
        monthButton = DUMonthButton()
        monthButton.frame = NSRect(x: 53, y: 2, width: 273, height: 40)
        monthButton.stringValue("---")
        monthButton.click = self
        monthButton.onlyId = "monthButton"
        addSubview(monthButton)
    }
    
    func update() {
        date = CalendarUtil.getNowDateEntity()
        monthButton.stringValue(date.solar.dateYearMonthZH())
        GlobalControl.shared.monthDay?.setMonth(year: date.solar.year, month: date.solar.month)
        GlobalControl.shared.dayDetail?.update(date: date)
    }
    
    func update(year:Int, month:Int) {
        date = CalendarUtil.getDateEntity(year: year, month: month)
        monthButton.stringValue(date.solar.dateYearMonthZH())
        GlobalControl.shared.monthDay?.setMonth(year: date.solar.year, month: date.solar.month)
        //GlobalControl.shared.dayDetail?.update(date: date)
    }
    
    func onClick(_ params: NSObject?) {
        let p = params as! BaseView
        if p.onlyId == "preButton" {
            let p1 = GlobalControl.shared.page
            if p1 == nil {
                return
            }
            if p1!.isSwitch() {
                let s = p1!.prevPage()
                if s != "" {
                    monthButton.stringValue(s)
                }
            } else {
                date = p1!.preMonth(date: date)
            }
        } else if p.onlyId == "nextButton" {
            let p1 = GlobalControl.shared.page
            if p1 == nil {
                return
            }
            if p1!.isSwitch() {
                let s = p1!.nextPage()
                if s != "" {
                    monthButton.stringValue(s)
                }
            } else {
                date = p1?.nextMonth(date: date)
            }
        } else if p.onlyId == "monthButton" {
            let s = GlobalControl.shared.page!.showYearMonth(year: date.solar.year, month: date.solar.month)
            if s != "" {
                monthButton.stringValue(s)
            }
        }
    }
}
